package cn.ihrm.sass.controller;

import cn.common.Result;
import cn.common.ResultCode;
import cn.controller.BaseController;
import cn.exception.CommonException;
import cn.ihrm.sass.entity.system.User;
import cn.ihrm.sass.response.ProfileResponse;
import cn.ihrm.sass.service.UserService;
import cn.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/7/4 17:00
 */
@RestController
@RequestMapping("login")
public class LoginController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private JWTUtils jwtUtils;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Result login(@RequestBody Map<String, String> loginMap) {
        String mobile = loginMap.get("mobile");
        String password = loginMap.get("password");
        User user = userService.findByMobile(mobile);
        //登录失败
        // user为空 或者密码不匹配
        if (user == null || !user.getPassword().equals(password)) {
            // 用户名或者密码错误
            return new Result(ResultCode.MOBILEERROR);
        } else {
            //登录成功 构造返回token
            Map<String, Object> map = new HashMap<>();
            map.put("companyId", user.getCompanyId());
            map.put("companyName", user.getCompanyName());
            String token = jwtUtils.createJWT(user.getId(), user.getUsername(), map);
            return new Result(ResultCode.SUCCESS, token);
        }

    }

    /**
     * 用户登录之后，获取用户信息
     * 获取用户id
     * 根据用户id 查询用户
     * 构造返回对象
     *
     * @return
     */
    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public Result profile(HttpServletRequest request) throws CommonException {
        /**
         * 从请求头中获取token的值
         *  替换 Bearer+空格
         *  解析token
         *  获取clamis
         */
        //String authorization = request.getHeader("Authorization");
        //if(authorization == null){
        //    boolean empty = StringUtils.isEmpty(authorization);
        //    throw new CommonException(ResultCode.UNAUTHENTICATED);
        //}
        // 替换为空
        //String token = authorization.replace("Bearer ","");
        // 解析token
        //Claims claims = jwtUtils.parseJWT(authorization);
        // 从request域中 拿到 claims
        //Claims claims = (Claims) request.getAttribute("user_claims");
        String userId = claims.getId();
        if(userId == null){
            throw new CommonException(ResultCode.USERIDISNULL);
        }
        User user = userService.findById(userId);
        // 构造用户返回对象
        ProfileResponse pr = new ProfileResponse(user);
        return new Result(ResultCode.SUCCESS, pr);
    }
}